package com.netease.yanxuan.qa.yx.yuheng.filter;

import com.netease.yanxuan.qa.yx.yuheng.config.YxYuhengConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * <Description>
 *
 * @author yanlu02
 * @date 2021/1/15 11:07
 */
@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ServiceFilter implements Filter {

    private Pattern skipPattern = Pattern.compile(YxYuhengConstants.DEFAULT_SKIP_PATTERN);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        log.info("uri={}", req.getRequestURI());
        if (req.getRequestURI().contains("swagger-") || req.getRequestURI().contains("api-docs") || req.getRequestURI().contains("health") || req.getRequestURI().contains(YxYuhengConstants.SERVICE_NAME)) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            log.error("url is not valid，uri={}", req.getRequestURI());
        }
    }

    @Override
    public void destroy() {

    }
}
